package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.Utils.Page;
import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.Notice;
import com.example.shoppingmall.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    //公告列表分页+条件查新+排序
    @RequestMapping("/noticelist")
    public String noticelist(@RequestParam(value = "ntitle",required = false) String ntitle, @RequestParam(value = "ntime",required = false) String ntime,
                             @RequestParam(value = "nupdtime",required = false) String nupdtime, @RequestParam(value = "nsort",required = false) String nsort,
                             @RequestParam(value = "pageno",defaultValue = "1") Integer pageno, @RequestParam(value = "pageSize",defaultValue = "6") Integer pageSize,
                             Model model){
        int count=noticeService.ncount(ntitle,ntime);
        int num=(Integer.valueOf(pageno)-1)*Integer.valueOf(pageSize);
        Page page=new Page();
        page.setPageno(Integer.valueOf(pageno));
        page.setPageCount(count);
        List<Notice> noticeList=noticeService.getNoticeall(ntitle,ntime,nupdtime,nsort,num,Integer.valueOf(pageSize));
        model.addAttribute("page",page);
        model.addAttribute("noticeList",noticeList);
        model.addAttribute("ntitle",ntitle);
        model.addAttribute("ntime",ntime);
        return "Operate/Noticelist";
    }

    //新增公告列表
    @MyLog(value = "运营管理->公告列表->添加")
    @RequestMapping("/addnotice")
    public String addnotice(@RequestParam("ntitle") String ntitle,@RequestParam("ncontent") String ncontent,
                            @RequestParam("nsort") String nsort){
        Notice notice=new Notice();
        notice.setNtitle(ntitle);
        notice.setNcontent(ncontent);
        notice.setNsort(Integer.valueOf(nsort));
        if(noticeService.addNotice(notice)>0){
            return "redirect:/notice/noticelist";
        }
        return "error";
    }

    //查询详情信息
    @MyLog(value = "运营管理->公告列表->查询详情")
    @RequestMapping("/selnotice")
    @ResponseBody
    public Map<Object,Object> selnotice(Integer id){
        Map<Object,Object> map=new HashMap<>();
        Notice notice=noticeService.selnotice(id);
        map.put("notices",notice);
        return map;
    }

    //编辑公告信息
    @MyLog(value = "运营管理->公告列表->编辑")
    @RequestMapping("/updNotice")
    public String updNotice(@RequestParam("ntitle") String ntitle, @RequestParam("ncontent") String ncontent,
                            @RequestParam("nsort") String nsort,@RequestParam("id") String id){
        Notice notice=new Notice();
        notice.setId(Integer.valueOf(id));
        notice.setNtitle(ntitle);
        notice.setNcontent(ncontent);
        notice.setNsort(Integer.valueOf(nsort));
        if(noticeService.updNotice(notice)>0){
            return "redirect:/notice/noticelist";
        }
        return "error";
    }

    //删除操作
    @MyLog(value = "运营管理->公告列表->删除")
    @RequestMapping("/delnotice")
    @ResponseBody
    public Map<Object,Object> delnotice(Integer id){
        Map<Object,Object> map=new HashMap<>();
        if(noticeService.delNotice(id)>0){
            map.put("delnotice","yes");
        }else{
            map.put("delnotice","no");
        }
        return map;
    }
}
